package com.haierCamera.customapplication.ui.main.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;

import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

/**
 * Created by luyujian on 2018/9/29.
 * 圆角ImageView视图
 */

public class RoundCornerImageView extends AppCompatImageView {
    private Xfermode xfermode;

    private int width;
    private int height;

    //外部设置的半径值
    private int cornerRadius;

    //边框宽度
    private int borderWidth;
    //边框颜色
    private int borderColor;

    //边框半径
    private float[] borderRadii;
    //图片半径
    private float[] srcRadii;

    //图片占的矩形区域
    private RectF srcRectF;
    //边框的矩形区域
    private RectF borderRectF;
    //是否添加边框
    private boolean isBorder =true;

    private Path path = new Path();
    private Paint paint = new Paint();

    public RoundCornerImageView(Context context) {
        this(context, null);
    }

    public RoundCornerImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundCornerImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        borderRadii = new float[8];
        srcRadii = new float[8];

        borderRectF = new RectF();
        srcRectF = new RectF();

        xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        calculateRadius();
        initBorderRectF();
        initSrcRectF();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.saveLayer(srcRectF, null, Canvas.ALL_SAVE_FLAG);
        float sx = 1.0f * (width - 2 * borderWidth) / width;
        float sy = 1.0f * (height - 2 * borderWidth) / height;
        // 缩小画布，使图片内容不被border、padding覆盖
        canvas.scale(sx, sy, width / 2.0f, height / 2.0f);
        super.onDraw(canvas);
        paint.reset();
        path.reset();

        path.addRoundRect(srcRectF, srcRadii, Path.Direction.CCW);

        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setXfermode(xfermode);
        canvas.drawPath(path, paint);
        paint.setXfermode(null);
        // 恢复画布
        canvas.restore();

        if (isBorder) {
            drawBorders(canvas);
        }
    }

    /**
     * 设置圆角半径
     *
     * @param cornerRadius 圆角半径
     */
    public void setCornerRadius(int cornerRadius) {
        this.cornerRadius = cornerRadius;
    }

    /**
     * 设置边框
     *
     * @param cornerRadius 圆角半径
     * @param borderWidth  边框宽度
     * @param borderColor  边框颜色
     */
    public void setBorder(int cornerRadius, int borderWidth, int borderColor) {
        this.isBorder = true;
        this.borderWidth = borderWidth;
        this.borderColor = borderColor;
        this.cornerRadius = cornerRadius;
    }

    /**
     * 绘制边框
     *
     * @param canvas 画布
     */
    private void drawBorders(Canvas canvas) {
        drawRectFBorder(canvas, borderWidth, borderColor, borderRectF, borderRadii);
    }

    /**
     * 绘制矩形边框
     *
     * @param canvas      画布
     * @param borderWidth 边框宽度
     * @param borderColor 边框颜色
     * @param rectF       矩形
     * @param radii       半径集合
     */
    private void drawRectFBorder(Canvas canvas, int borderWidth, int borderColor, RectF rectF, float[] radii) {
        initBorderPaint(borderWidth, borderColor);
        path.addRoundRect(rectF, radii, Path.Direction.CCW);
        canvas.drawPath(path, paint);
    }

    /**
     * 初始画笔
     *
     * @param borderWidth 边框宽度
     * @param borderColor 边框颜色
     */
    private void initBorderPaint(int borderWidth, int borderColor) {
        path.reset();
        paint.setStrokeWidth(borderWidth);
        paint.setColor(borderColor);
        paint.setStyle(Paint.Style.STROKE);
    }

    /**
     * 计算圆角半径
     */
    private void calculateRadius() {
        for (int i = 0; i < borderRadii.length; i++) {
            borderRadii[i] = cornerRadius;
            srcRadii[i] = cornerRadius - borderWidth / 2.0f;
        }
    }
    /**
     * 计算外边框的RectF
     */
    private void initBorderRectF() {
        borderRectF.set(borderWidth / 2.0f, borderWidth / 2.0f, width - borderWidth / 2.0f, height - borderWidth / 2.0f);
    }
    /**
     * 计算图片原始区域的RectF
     */
    private void initSrcRectF() {
        srcRectF.set(0, 0, width, height);
    }
}
